import time


def my_middleware(get_response):
    print('my_middleware init 被调用')

    def middleware(request):
        print('before request 被调用')
        response = get_response(request)
        print('after response 被调用')
        return response

    return middleware


def time_middleware(get_response):
    print('用于对视图函数测试的中间件')

    def middleware(request):
        print('before request 被调用')
        start_time = time.time()
        # 调用视图函数之前 我记录一下当前的时间
        response = get_response(request)
        # 模拟耗时的操作
        time.sleep(1)
        end_time = time.time()
        print(end_time)
        print(f'本次请求所耗时间:{end_time - start_time} 秒 ', )
        return response

    return middleware


# 多个中间件 执行流程测试
def my_middleware1(get_response):
    print('init1 被调用')

    def middleware(request):
        print('before request 1 被调用')
        response = get_response(request)
        print('after response 1 被调用')
        return response

    return middleware


def my_middleware2(get_response):
    print('init2 被调用')
    def middleware(request):
        print('before request 2 被调用')
        response = get_response(request)
        print('after response 2 被调用')
        return response

    return middleware
